local Vector2 = UnityEngine.Vector2

Rect = {}

function Rect.New(x,y,width,height)
    local rt = {x = x, y = y, width = width, height = height}
    setmetatable(rt, Rect)
    return rt
end

function Rect:Get()
    return self.x, self.y, self.width, self.height
end

function Rect:Equals(other)
    return self.x == other.x and self.y == other.y and self.width == other.width and self.height == other.height
end

function Rect:Set(x,y,width,height)
    self.x = x
    self.y = y
    self.width = width
    self.height = height
end

function Rect:GetSize()
    return Vector2.New(self.width,self.height)
end
function Rect:SetSize(width,height)
    self.width = width
    self.height = height
end

function Rect:GetPosition()
    return Vector2.New(self.x,self.y)
end
function Rect:SetPosition(x,y)
    self.x = x
    self.y = y
end

function Rect:GetCenter()
    return Vector2.New((self.x+self.width)/2,(self.y+self.height)/2)
end
function Rect:SetCenter(x,y)
    return Vector2.New(x-self.width/2,y-self.height/2)
end

function Rect:Contains(point, allowInverse)
    allowInverse = allowInverse or true

    local _x,_y,_w,_h = self.x,self.y,self.width,self.height

    if allowInverse then
        if width < 0 then
            _w = - self.width
            _x = _x - _w
        end
        if height < 0 then
            _h = - self.height
            _y = _y - self.height
        end
    end

    if point.x >= self.x and point.y >= self.y and point.x <= self.x + self.width and point.y <= self.y - self.height then
        return true
    end
    return false
end


Rect.__tostring = function(self)
    return '(x:'..self.x..', y:'..self.y..', width:'..self.width..', height:'..self.height..')'
end

setmetatable(Rect, Rect)
AddValueType(Rect, 13)
UnityEngine.Rect = Rect

return Rect